package com.cll.learn.cas.demo1;

/**
 * 描述信息:
 *
 * @author CLL
 * @version 1.0
 * @date 2021/5/15 11:09
 */
public class ConsumeThread implements Runnable {

    private final User user;

    public ConsumeThread(User user) {
        this.user = user;
    }

    @Override
    public void run() {
        while (true) {
            Integer currentMoney = user.getMoney().get();
            if (currentMoney > 10) {
                if (user.getMoney().compareAndSet(currentMoney, currentMoney - 10)) {
                    System.out.println("===>>> 当前用户余额 = " + currentMoney + "：消费成功之后余额" + user.getMoney().get());
                    break;
                }
            } else {
//                System.out.println("===>>> 用户余额不足");
                break;
            }
        }

        try {
            Thread.sleep(100);
        } catch (InterruptedException ie) {

        }
    }
}
